<template>
  <div class="login-page">
    <div class="bg">
      <div class="bg-overlay"></div>
    </div>
    <div class="content-container">
      <!-- 统一的半透明背景板 -->
      <div class="unified-bg-board">
        <!-- logo 容器 -->
        <div class="logo-container">
          <img src="@/assets/logo4.png" alt="Logo" class="logo">
        </div>
        <!-- 表单容器 -->
        <div class="form-container">
          <transition name="form-slide" mode="out-in">
            <!-- 注册表单 -->
            <el-form ref="form" size="large" autocomplete="off" v-if="isRegister" :model="registerDate" :rules="rules">
              <el-form-item>
                <h1 class="title">慧家房产 - 注册</h1>
              </el-form-item>
              <el-form-item prop="username">
                <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="registerDate.username"></el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="registerDate.password"></el-input>
              </el-form-item>
              <el-form-item prop="repassword">
                <el-input :prefix-icon="Lock" type="password" placeholder="请输入再次密码" v-model="registerDate.repassword"></el-input>
              </el-form-item>
              <el-form-item>
                <el-select v-model="registerDate.roleId" placeholder="请选择角色">
                  <el-option label="客户" value="ROLE_customer" />
                  <el-option label="房东" value="ROLE_landlord" />
<!--                  <el-option label="中介" value="ROLE_intermediary" />-->
                </el-select>
              </el-form-item>
              <!-- 注册按钮 -->
              <el-form-item>
                <el-button class="button" type="primary" auto-insert-space @click="register">
                  注册
                </el-button>
              </el-form-item>
              <el-form-item class="flex">
                <el-link type="info" :underline="false" @click="isRegister = false;clearRegisterData()">
                  ← 返回
                </el-link>
              </el-form-item>
            </el-form>
            <!-- 登录表单 -->
            <el-form ref="form" size="large" autocomplete="off" v-else :model="registerDate" :rules="rules">
              <el-form-item>
                <h1 class="title">慧家房产 - 登录</h1>
              </el-form-item>
              <el-form-item prop="username">
                <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="registerDate.username"></el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input name="password" :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="registerDate.password"></el-input>
              </el-form-item>
              <el-form-item>
                <el-select v-model="registerDate.roleId" placeholder="请选择角色">
                  <el-option label="客户" value="ROLE_customer" />
                  <el-option label="房东" value="ROLE_landlord" />
                  <el-option label="中介" value="ROLE_intermediary" />
                </el-select>
              </el-form-item>
              <el-form-item class="flex">
                <div class="flex">
                </div>
              </el-form-item>
              <!-- 登录按钮 -->
              <el-form-item>
                <el-button class="button" type="primary" auto-insert-space @click="login">登录</el-button>
              </el-form-item>
              <el-form-item class="flex">
                <el-link type="info" :underline="false" @click="isRegister = true;clearRegisterData()">
                  注册 →
                </el-link>
              </el-form-item>
            </el-form>
          </transition>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
// 控制注册与登录表单的显示， 默认显示登录
const isRegister = ref(false)
// 定义数据模型
const registerDate = ref({
  username: '',
  password: '',
  repassword: '',
  roleId: ''
})
// 校验密码函数
const checkrePassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次确认密码'))
  } else if (value !== registerDate.value.password) {
    callback(new Error('两次输入密码不一样'))
  } else {
    callback()
  }
}

// 表单校验规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 16, message: '长度为 5 - 16 位非空字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 5, max: 16, message: '长度为 5 - 16 位非空字符', trigger: 'blur' }
  ],
  repassword: [
    { validator: checkrePassword, trigger: 'blur' }
  ]
}

// 调用注册接口完成注册  注册按钮
import { userRegisterService, userLoginService } from '@/api/user.js'
import { useRouter } from 'vue-router'

const router = useRouter()
const register = async () => {
  // registerDate是响应式对象 如果想获取值 需要.value
  let result = await userRegisterService(registerDate.value);
  ElMessage.success('注册成功')
}

import { useTokenStore } from "@/stores/token.js";

const tokenStore = useTokenStore();
// 登录接口
const login = async () => {
  let result = await userLoginService(registerDate.value);
  ElMessage.success('登录成功')
  // 得到的token存储到pinia中
  tokenStore.setToken(result.res.token)
  // 跳转到首页，路由完成
  if (result.res.roleId === 'ROLE_admin') {
    console.log("管理员登录")
    router.push("/admin")
  } else if (result.res.roleId === 'ROLE_customer') {
    console.log("客户登录")
    router.push("/customer")
  } else if (result.res.roleId === 'ROLE_landlord') {
    console.log("房东登录")
    router.push("/landlord")
  } else if (result.res.roleId === 'ROLE_intermediary') {
    console.log("中介登录")
    router.push("/intermediary")
  }
}
// 清空模型数据
const clearRegisterData = () => {
  registerDate.value = {
    username: "",
    password: "",
    repassword: "",
    roleId: ""
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #001529;
  overflow: hidden; /* 解决滚动条问题 */
}

.bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url('@/assets/logobg.jpg') no-repeat center / cover;
  animation: fadeIn 1s ease;
}

.bg-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(5px);
}

.content-container {
  position: relative;
  z-index: 1;
  display: flex;
  width: 80%;
  max-width: 1200px;
  justify-content: center;
  align-items: center;
}

.unified-bg-board {
  width: 90%;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 500px; /* 添加最小高度 */
}

.logo-container {
  margin-left: 40px;
  width: 25%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.logo {
  max-width: 100%;
  height: auto;
}

.form-container {
  width: 35%;
  margin-right: 50px;
}

.title {
  margin: 0 auto;
  color: #1890ff;
  text-shadow: 0 0 10px rgba(24, 144, 255, 0.5);
  text-align: center;
  margin-bottom: 20px;
}

.button {
  width: 100%;
  background-color: #1890ff;
  border-color: #1890ff;
  transition: all 0.3s ease;

  &:hover {
    background-color: #40a9ff;
    border-color: #40a9ff;
    box-shadow: 0 0 10px rgba(24, 144, 255, 0.5);
  }
}

.flex {
  width: 100%;
  display: flex;
  justify-content: space-between;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* 表单左右滑动动画 */
.form-slide-enter-active {
  animation: slideInRight 0.6s ease;
}

.form-slide-leave-active {
  animation: slideOutLeft 0.6s ease;
}

@keyframes slideInRight {
  from {
    opacity: 0;
    transform: translateX(100%);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes slideOutLeft {
  from {
    opacity: 1;
    transform: translateX(0);
  }
  to {
    opacity: 0;
    transform: translateX(-100%);
  }
}
</style>
